package cn.itcast.test.kafka;

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;
import org.junit.Test;

import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

public class ConsumerQuickStart {
    @Test
    public void onMsg() {
        //2. 完成客户端的配置
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.130:9092");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "CD58");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");

        //1. 创建核心对象
        KafkaConsumer consumer = new KafkaConsumer<String, String>(properties);
        //3. 订阅一个或者多个topic
        consumer.subscribe(Collections.singletonList("order"));
        //4. 拉取消息进行消费
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMinutes(1000));

            for (ConsumerRecord<String, String> record : records) {
                System.out.println(record.key() + "--->" + record.value());
            }
        }
    }

    //同步提交，逐条消费，速度慢，需要错误可以一直重新尝试
    @Test
    public void onMsgSync() {
        //2. 完成客户端的配置
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.130:9092");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "CD58");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,false);
        //1. 创建核心对象
        KafkaConsumer consumer = new KafkaConsumer<String, String>(properties);
        //3. 订阅一个或者多个topic
        consumer.subscribe(Collections.singletonList("order"));
        //4. 拉取消息进行消费
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMinutes(1000));

            for (ConsumerRecord<String, String> record : records) {
                System.out.println(record.key() + "--->" + record.value());
                //同步提交
                consumer.commitSync();
            }
        }
    }

    //异步提交，异步消费，速度快，但是遇到错误就会终止
    @Test
    public void onMsgAsync() {
        //2. 完成客户端的配置
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.130:9092");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "CD58");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,false);
        //1. 创建核心对象
        KafkaConsumer consumer = new KafkaConsumer<String, String>(properties);
        //3. 订阅一个或者多个topic
        consumer.subscribe(Collections.singletonList("order"));
        //4. 拉取消息进行消费

        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMinutes(1000));

            for (ConsumerRecord<String, String> record : records) {
                System.out.println(record.key() + "--->" + record.value());
            }

            //异步提交
            consumer.commitAsync(new OffsetCommitCallback() {
                @Override
                public void onComplete(Map<TopicPartition, OffsetAndMetadata> map, Exception e) {
                    if (e != null){
                        System.out.println("记录错误的提交偏移量："+ map+",异常信息"+e);
                    }
                }
            });
        }
    }

    //同步和异步提交配合使用
    @Test
    public void onMsg2() {
        //2. 完成客户端的配置
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.130:9092");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "CD58");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,false);
        //1. 创建核心对象
        KafkaConsumer consumer = new KafkaConsumer<String, String>(properties);
        //3. 订阅一个或者多个topic
        consumer.subscribe(Collections.singletonList("order"));
        //4. 拉取消息进行消费

        try {
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMinutes(1000));

                for (ConsumerRecord<String, String> record : records) {
                    System.out.println(record.key() + "--->" + record.value());
                    //异步提交
                    consumer.commitAsync();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                consumer.commitSync();
            }finally {
                consumer.close();
            }
        }
    }
}
